#!/bin/sh

# A helper script to run apt-get noninteractively with the right settings.
# Run as one of:
#   run-apt-get update
#   run-apt-get upgrade-package packagename
#   run-apt-get upgrade-all

set -e

DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND

options="-o DPkg::Options::=--force-confdef -o DPkg::Options::=--force-confold -o DPkgPM::Progress=0"

keyring=/etc/apt/trusted.gpg.d/flightaware-archive-keyring.gpg
keyring_source=/usr/lib/piaware/helpers/flightaware-archive-keyring.gpg
sourceslist=/etc/apt/sources.list.d/piaware.list
packagedsourceslist=

action="$1"
shift

. /etc/os-release
case "$ID:$VERSION_ID" in
    raspbian:7)
        url=http://flightaware.com/adsb/piaware/files/packages
        suite=wheezy
        components=piaware
        ;;

    raspbian:8)
        url=http://flightaware.com/adsb/piaware/files/packages
        suite=jessie
        components=piaware
        packagedsourceslist=/etc/apt/sources.list.d/piaware-jessie.list
        ;;

    raspbian:9)
        url=http://flightaware.com/adsb/piaware/files/packages
        suite=stretch
        components=piaware
        packagedsourceslist=/etc/apt/sources.list.d/piaware-stretch.list
        ;;

    raspbian:10)
        url=http://flightaware.com/adsb/piaware/files/packages
        suite=buster
        components=piaware
        packagedsourceslist=/etc/apt/sources.list.d/piaware-buster.list
        ;;

    *)
        echo "Automatic updates not available for OS $ID:$VERSION_ID ($PRETTY_NAME)" >&2
        exit 1
esac

# ensure the keyring and sources list are installed
# (they may not be if this is a non-sdcard install
# or a sdcard upgrade)

if [ ! -e "$keyring" ]
then
    echo "$keyring does not exist; installing it"
    ln -s "$keyring_source" "$keyring"
fi

if [ ! -e "$sourceslist" ] && [ ! -e "$packagedsourceslist" ]
then
    echo "$sourceslist does not exist; creating it"
    echo "deb $url $suite $components" >$sourceslist
fi

case "$action" in
    update)
        dpkg --configure -a --force-confdef --force-confold || true
        apt-get -q -y $options update
        ;;

    upgrade-package)
        dpkg --configure -a --force-confdef --force-confold || true
        apt-get -q -y $options -f install || true
        apt-get -q -y $options install "$@" || exit 1
        apt-get -q $options clean || true
        ;;

    upgrade-all)
        dpkg --configure -a --force-confdef --force-confold || true
        apt-get -q -y $options -f install || true
        apt-get -q -y $options dist-upgrade || exit 1
        apt-get -q -y $options clean || true
        ;;

    *)
        echo "Unhandled action: $action"
        exit 1
        ;;
esac

exit 0
